﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IRepository;//引用接口的命名空间
using WorkModel;
using Dapper;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;

namespace Repository//[实现接口类]
{
  public  class JueSeintfas: IJueSeintfas//继承角色接口
    {
        //全局变量 相当于上下文1
        public IDbConnection conn;
        //定义一个连接字符串
        private string ma = "";
        //依赖注入包
        private IConfiguration mb = null;
        //构造函数
        public JueSeintfas(IConfiguration mc)
        {
            //把依赖注入的包注入到构造函数里
            mb = mc;
            //转换SQL语句
            Dapper.SimpleCRUD.SetDialect(SimpleCRUD.Dialect.SQLServer);
            //连接字符串
            ma = mb.GetConnectionString("DBconn");
            //全局变量 相当于上下文2
            conn = new SqlConnection(ma);
        }
        /// <summary>
        /// 角色显示
        /// </summary>
        /// <returns></returns>
        public List<Juese> JueSeShow(string name, int cIndex, int pSize, out int Count)
        {
            //计算总条数
            Count = conn.RecordCount<Juese>();
            //查询
            if (name != "")
            {
                string sql = $"select * from   Juese  where Jname = '{name}' ";
                return conn.Query<Juese>(sql).ToList();
            }
            return conn.GetListPaged<Juese>(cIndex, pSize, "", "").ToList();
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int JuSerDelete(string id)
        {
            return conn.Delete<Juese>(id);
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int JuSerAdd(Juese m)
        {
            return (int)conn.Insert<Juese>(m);
        }
        /// <summary>
        /// 反填角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Juese JuSerFan(int id)
        {
            return conn.Get<Juese>(id);
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int JuSerUpt(Juese m)
        {
            return conn.Update<Juese>(m);
        }
        //----------------------------------系统表---------------------------------------------
        /// <summary>
        /// 系统显示
        /// </summary>
        /// <param name="Begintime"></param>
        /// <param name="Overtime"></param>
        /// <param name="cIndex"></param>
        /// <param name="pSize"></param>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<Xitong> XTShow(DateTime Begintime, DateTime Overtime, int cIndex, int pSize, out int Count)
        {
            Count = conn.RecordCount<Xitong>();//查询总条数
           
            return conn.GetListPaged<Xitong>(cIndex, pSize, "", "").ToList();
        }
        /// <summary>
        /// 删除系统表里的数据(支持批量删除)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int XiTongDelete(string id)
        {
            string n = $"delete from Xitong where Xid in ({id}) ";
            return conn.Execute(n);
        }
        /// <summary>
        /// 系统表修改状态1
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int XiTongShi1(string id)
        {
            string n = $"update  Xitong set Xint=1 where Xid in ({id})";
            return conn.Execute(n);
        }
        /// <summary>
        /// 系统表修改状态0
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int XiTongShi0(string id)
        {
            string n = $"update  Xitong set Xint=0 where Xid in ({id})";
            return conn.Execute(n);
        }
        /// <summary>
        /// 系统表添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int XiTongAdd(Xitong m)
        {
            return (int)conn.Insert<Xitong>(m);
        }
        /// <summary>
        /// 系统表反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Xitong XiTongFan(int id)
        {
            return conn.Get<Xitong>(id);
        }
        /// <summary>
        /// 系统表修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int XiTongUpt(Xitong m)
        {
            return conn.Update<Xitong>(m);
        }

        //-------------------------字典表----------------------------------
        /// <summary>
        /// 字典表显示+查询+分页
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cIndex"></param>
        /// <param name="pSize"></param>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<ZiDian> ZDShow(string name, int cIndex, int pSize, out int Count)
        {
            Count = conn.RecordCount<ZiDian>();//计算总数
            if (name!="")
            {
                string f = $"select * from ZiDian where Ztype='{name}'";
                return conn.Query<ZiDian>(f).ToList();
            }
            return conn.GetListPaged<ZiDian>(cIndex, pSize, "", "").ToList();
        }
        /// <summary>
        /// 字典表添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int ZDAdd(ZiDian m)
        {
            return (int)conn.Insert<ZiDian>(m);
        }
        /// <summary>
        /// 字典表反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ZiDian ZDFan(int id)
        {
            return conn.Get<ZiDian>(id);
        }
        /// <summary>
        /// 字典表修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int ZDUpt(ZiDian m)
        {
            return conn.Update<ZiDian>(m);
        }
        /// <summary>
        /// 字典表删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int ZDDelete(string id)
        {
            return conn.Delete<ZiDian>(id);
        }
        /// <summary>
        /// 字典表删除(批删)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int ZDDeleteAll(string id)
        {
            string s = $"delete from ZiDian where Zid in ({id})";
            return conn.Execute(s);
        }
        //---------------------请假表---------------------------
        /// <summary>
        /// 请假显示
        /// </summary>
        /// <param name="Lbegintime"></param>
        /// <param name="Lovertime"></param>
        /// <param name="cIndex"></param>
        /// <param name="pSize"></param>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<Leave> LeaveShow(string bh, /*DateTime Lbegintime, DateTime Lovertime,*/ int cIndex, int pSize, out int Count)
        {
            Count = conn.RecordCount<Leave>();//计算总数
            if (bh!="")
            {
                string f = $"select * from Leave where Lnum='{bh}'";
                return conn.Query<Leave>(f).ToList();
            }
           
            return conn.GetListPaged<Leave>(cIndex, pSize, "", "").ToList();
        }
        /// <summary>
        /// 添加请假
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int LeaveAdd(Leave m)
        {
            return (int)conn.Insert<Leave>(m);
        }
        /// <summary>
        /// 修改请假
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int LeaveUpt(Leave m)
        {
            return conn.Update<Leave>(m);
        }
        /// <summary>
        /// 请假反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Leave LeaveFan(int id)
        {
            return conn.Get<Leave>(id);
        }
        /// <summary>
        /// 删除请假条(支持批量删除)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int LeaveDelete(string id)
        {
            string s = $"delete from Leave where Lid in ({id})";
            return conn.Execute(s);
        }










    }
}
